/*  */
$(function(){
    App.init();
    if (jQuery().datepicker) {
        $('.date-picker').datepicker({
            language:"zh-CN",
            rtl: App.isRTL(),
            autoclose: true
        });
    }
    //新增
    var $addBtn = $( '#addNewPoint' ),
        $description = $( '#descriptionAdd' ),
        $startPoints = $( '#startPointsAdd' ),
        $endPoints = $( '#endPointsAdd' ),
        $basePoints = $( '#basePointsAdd' ),
        $remarks = $( '#remarksAdd' ),
        $startDate = $( '#startDateAdd' ),
        $endDate = $( '#endDateAdd' ),
        $addTB = $( '#addTable' ),
        $listTB = $( '#listTable' );

    //判断是否为空
    function isInputNull( $obj, message ){
        if( $.trim( $obj.val() ).length === 0 ){
            $.Alert( message );
            return false;
        };
        return true;
    };
    //判断是否为数字
    function isValNan( $obj, message ){
        if( isNaN( $.trim($obj.val()) ) ){
            $.Alert( message );
            return false;
        };
        return true;
    };
    //判断数值大小
    function isGreater( $startObj, $endObj, message ){
        var startNum = $.trim( $startObj.val() ),
            endNum = $.trim( $endObj.val() );
        if( isNaN( startNum ) || isNaN( endNum ) ) return;
        if( startNum > endNum  ){
            $.Alert( message );
            return false;
        };
        return true;
    };
    //克隆到上一个table,并生成对应input
    function addNewItem(){
        var i = 1, $tr = '<tr>';
        $tr += '<td><span class="tb-val">'+ $.trim( $addTB.find( 'td:eq(0)' ).text() ) +'</span><input type="text" class="form-control hide" value="'+ $addTB.find( 'td:eq(0)' ).text() +'"></td>';
        $tr += '<td><span class="tb-val">'+ $.trim( $description.val() ) +'</span><input type="text" class="form-control hide" value="'+ $.trim( $description.val() ) +'"></td>';
        $tr += '<td><span class="tb-val">'+ $.trim( $startPoints.val() ) +'</span><input type="text" class="form-control hide" value="'+ $.trim( $startPoints.val() ) +'"></td>';
        $tr += '<td><span class="tb-val">'+ $.trim( $endPoints.val() ) +'</span><input type="text" class="form-control hide" value="'+ $.trim( $endPoints.val() ) +'"></td>';
        $tr += '<td><span class="tb-val">'+ $.trim( $basePoints.val() ) +'</span><input type="text" class="form-control hide" value="'+ $.trim( $basePoints.val() ) +'"></td>';
        $tr += '<td><span class="tb-val">'+ $.trim( $remarks.val() ) +'</span><input type="text" class="form-control hide" value="'+ $.trim( $remarks.val() ) +'"></td>';
        $tr += '<td><span class="tb-val-date"><span class="tb-val-start">'+ $startDate.val() + '</span> 至 <span class="tb-val-end">' +  $endDate.val() + '</span></span><div class="input-group input-large date-picker input-daterange" data-date-format="yyyy-mm-dd" style="display:none;"><input type="text" class="form-control" name="from" value="'+ $startDate.val() +'"><span class="input-group-addon">至</span><input type="text" class="form-control" name="to" value="'+ $endDate.val() +'"></div></td>';
        $tr += '<td><button type="button" class="tb-save btn btn-sm green" style="display:none;"><i class="icon-save"></i> 保存</button><button type="button" class="tb-edit btn btn-xs blue margin-right-10">修改</button><button type="button" class="tb-delete btn btn-xs red">删除</button></td>';
        $tr += '</tr>'
        $listTB.find( 'tbody' ).append( $tr );
        $startDate.val( '' );
        $endDate.val( '' );
    };
    //保存表格数据
    function saveItem( $obj ){
        //显示修改后的值，隐藏输入框
        $obj.find( 'span.tb-val' ).each(function( index, item ){
            var val = $.trim( $(this).siblings( 'input.form-control' ).val() );
            $(this).text( val ).addClass( 'show' ).removeClass( 'hide' ).siblings( 'input.form-control' ).removeClass( 'show' ).addClass( 'hide' );
        });
        $obj.find( 'span.tb-val-date' ).show().children( 'span.tb-val-start' ).text( $obj.find( '.date-picker input' ).eq(0).val() ).siblings( 'span.tb-val-end' ).text( $obj.find( '.date-picker input' ).eq(1).val() );
        $obj.find( '.date-picker, .tb-save' ).hide();
        $obj.find( 'button.tb-edit, button.tb-delete' ).show();
    };
    //点击新增
    $addBtn.on( 'click', function(){
        isInputNull( $description, '请输入项目细分描述！' ) &&
        isInputNull( $startPoints, '请输入加分起始值!' ) &&
        isInputNull( $endPoints, '请输入加分终止值!' ) &&
        isInputNull( $basePoints, '请输入奖励积分值!' ) &&
        isInputNull( $remarks, '请输入备注!' ) &&
        isInputNull( $startDate, '请输入起始日期!' ) &&
        isInputNull( $endDate, '请输入终止日期!' ) &&
        isValNan( $startPoints, '加分起始值只能是数字！' ) &&
        isValNan( $endPoints, '加分终止值只能是数字!' ) &&
        isValNan( $basePoints, '"数值型"奖励积分值只能是数字!' ) &&
        isGreater( $startPoints, $endPoints, '加分终止值必须大于等于加分起始值' ) &&
        addNewItem();
    });
    //点击修改
    $listTB.on( 'click', '.tb-edit', function(){
        //隐藏值，显示输入框
        var $curTr = $(this).closest( 'tr' );
        $curTr.find( 'span.tb-val' ).removeClass( 'show' ).addClass( 'hide' ).siblings( 'input.form-control' ).removeClass( 'hide' ).addClass( 'show' );
        $curTr.find( '.date-picker, .tb-save' ).show();
        $curTr.find( 'span.tb-val-date, button.tb-edit, button.tb-delete' ).hide();
        //重置date-picker
        if (jQuery().datepicker) {
            $('.date-picker').datepicker({
                language:"zh-CN",
                rtl: App.isRTL(),
                autoclose: true
            });
        };
    });
    //点击保存
    $listTB.on( 'click', '.tb-save', function(){
        var $curTr = $(this).closest( 'tr' );
        isInputNull( $curTr.find( 'input.form-control' ).eq(0), '请输入积分奖励类型！' ) &&
        isInputNull( $curTr.find( 'input.form-control' ).eq(1), '请输入项目细分描述！' ) &&
        isInputNull( $curTr.find( 'input.form-control' ).eq(2), '请输入加分起始值!' ) &&
        isInputNull( $curTr.find( 'input.form-control' ).eq(3), '请输入加分终止值!' ) &&
        isInputNull( $curTr.find( 'input.form-control' ).eq(4), '请输入奖励积分值!' ) &&
        isInputNull( $curTr.find( 'input.form-control' ).eq(5), '请输入备注!' ) &&
        isInputNull( $curTr.find( 'input.form-control' ).eq(6), '请输入起始日期!' ) &&
        isInputNull( $curTr.find( 'input.form-control' ).eq(7), '请输入终止日期!' ) &&
        isValNan( $curTr.find( 'input.form-control' ).eq(2), '加分起始值只能是数字！' ) &&
        isValNan( $curTr.find( 'input.form-control' ).eq(3), '加分终止值只能是数字!' ) &&
        isValNan( $curTr.find( 'input.form-control' ).eq(4), '"数值型"奖励积分值只能是数字!' ) &&
        isGreater( $curTr.find( 'input.form-control' ).eq(2), $listTB.find( 'input.form-control' ).eq(3), '加分终止值必须大于等于加分起始值' ) &&
        saveItem( $curTr );
    });
    //点击删除
    $listTB.on( 'click', '.tb-delete', function(){
        $(this).closest( 'tr' ).remove();
    });
})